package cn.kent.simple;

/**
 * 283. 移动零
 *
 * 将所有0移动到后面位置，不动用其他元素顺序
 */
public class MoveZeroes {
    public static void main(String[] args) {
        int[] nums = {0, 1, 0, 3, 12};
        moveZeroes2(nums);
        for (int num : nums) {
            System.out.println(num);
        }
    }

    /**
     * 双指针
     * 移动
     */
    public static void moveZeroes2(int[] nums) {
        int left = 0, right = 0;
        int len = nums.length;
        while (right < len) {
            if (nums[right] != 0) {
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
                left++;
            }
            right++;
        }
    }

    /**
     * 1ms 100%
     */
    public static void moveZeroes(int[] nums) {
        int i = 0;
        for (int num : nums) {
            if (num != 0) {
                nums[i] = num;
                i++;
            }
        }
        for (; i < nums.length; i++) {
            nums[i] = 0;
        }
    }
}
